package com.drinks.yiniang.adapter.listview;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.user.milepost.WineAnalyseJson;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

/**
 * 里程碑酒类分析 adapter
 *
 * Created by han on 2019/6/27 0027.
 */

public class WineAnalyseAdapter extends BaseAdapter {
    private Context mContext;

    private List<WineAnalyseJson.WineAnalyseList> mList;

    private LayoutInflater inflater;

    private AppCompatTextView mTvDataView;

    private SpannableStringBuilder mSpanns;

    private int start, end;
    private int mSp20;

    public WineAnalyseAdapter(Context mContext, List<WineAnalyseJson.WineAnalyseList> mList) {
        this.mContext = mContext;
        this.mList = mList;

        inflater = LayoutInflater.from(mContext);

        mSpanns = new SpannableStringBuilder();

        mSp20 = ScreenSizeUtils.sp2Px(mContext, 20);
    }

    @Override
    public int getCount() {
        return CommonUtils.getListSize(mList);
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;

        if (convertView == null) {
            mHolder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_wine_analyse, null);

            mHolder.tv_wine_analyse_name = convertView.findViewById(R.id.tv_wine_analyse_name);
            mHolder.tv_wine_analyse_num = convertView.findViewById(R.id.tv_wine_analyse_num);

            mHolder.ll_wine_analyse_data = convertView.findViewById(R.id.ll_wine_analyse_data);

            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        WineAnalyseJson.WineAnalyseList item = mList.get(position);

        setName(mHolder.tv_wine_analyse_name, item.getName());
        setNum(mHolder.tv_wine_analyse_num, item.getTotal());
        addData(mHolder.ll_wine_analyse_data, item.getStyles());

        return convertView;
    }

    private void setName(TextView tv, String name) {
        mSpanns.clearSpans();
        mSpanns.clear();
        mSpanns.append(StringUtils.getNullEmptyConvert__(name));
        mSpanns.append("分析");

        tv.setText(mSpanns);
    }

    private void setNum(TextView tv, String num) {
        mSpanns.clearSpans();
        mSpanns.clear();

        mSpanns.append("喝过");
        start = mSpanns.length();
        mSpanns.append(StringUtils.getNullEmptyConvertZero(num));
        end = mSpanns.length();
        mSpanns.append("瓶");
        mSpanns.setSpan(new ForegroundColorSpan(ColorUtils.getColorById(mContext, R.color.color_main)), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mSpanns.setSpan(new AbsoluteSizeSpan(mSp20), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mSpanns.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        tv.setText(mSpanns);
    }

    private void addData(AutoLineFeedLayout layout, List<WineAnalyseJson.WineAnalyseStyles> styles) {
        layout.removeAllViews();

        if(CommonUtils.isEmptyList(styles)) {
            return;
        }

        for(int i = 0; i < styles.size(); i++) {
            layout.addView(getTextData(styles.get(i).getName(), styles.get(i).getPercent()));
        }
    }

    private AppCompatTextView getTextData(String name, String value) {
        mSpanns.clearSpans();
        mSpanns.clear();

        mSpanns.append(StringUtils.getNullEmptyConvert__(name));
        mSpanns.append("  ");
        start = mSpanns.length();
        mSpanns.append(StringUtils.getNullEmptyConvertZero(value));
        mSpanns.append("%");
        end = mSpanns.length();
        mSpanns.setSpan(new ForegroundColorSpan(ColorUtils.getColorById(mContext, R.color.color_282829)), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mSpanns.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        mTvDataView = new AppCompatTextView(mContext);

        mTvDataView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        mTvDataView.setTextColor(ColorUtils.getColorById(mContext, R.color.color_b0b0b0));
        mTvDataView.setText(mSpanns);

        return mTvDataView;
    }

    class ViewHolder {
        //名称
        TextView tv_wine_analyse_name;
        //数量
        TextView tv_wine_analyse_num;
        //数据
        AutoLineFeedLayout ll_wine_analyse_data;
    }
}
